package com.yyk.yiliao.util;

import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;

import com.yyk.yiliao.config.MyApplication;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/9/28 0028.
 * Android 监听 EditText 使 Button 是否能可用工具类 - Android - 掘金  https://juejin.im/entry/58b78ecb61ff4b006cd48de6
 * <p>
 * 其他个人信息使用
 */
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public class BtnToEditListenerUtil {
//    private int click = R.drawable.blue_51_6dip_bg_bt;  //默认有的
//    private int noClick = R.drawable.blue_8b_6dip_bg_bt;
//    private int click = R.drawable.blue_51_36dip_bg_bt;
//    private int noClick = R.drawable.blue_8b_36dip_bg_bt;

    private int click;
    private int noClick;

    private List<EditText> editTextList = new ArrayList<>();

    private Button btn;

    public static BtnToEditListenerUtil getInstance() {
        return new BtnToEditListenerUtil();
    }

    public BtnToEditListenerUtil setBtn(Button btn) {
        this.btn = btn;
        btn.setEnabled(false);
        return this;
    }

    public BtnToEditListenerUtil setBackgroud(int click, int noClick) {
        this.click = click;
        this.noClick = noClick;
        return this;
    }

    public BtnToEditListenerUtil addEditView(EditText editText) {
        editTextList.add(editText);
        return this;
    }

    public void build() {

        setWatcher();
    }

    /**
     * 给每一个EditText设置Watcher监听，当前的EditText文本不为空时遍历每一个EditText，
     * 只有都不为空时 tag为true
     */
    private void setWatcher() {

        for (int i = 0; i < editTextList.size(); i++) {
            editTextList.get(i).addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (s.length() == 0) {
                        setBtnUnavailable();
                        return;
                    }

                    boolean tag = false;

                    for (int i = 0; i < editTextList.size(); i++) {
                        if (editTextList.get(i).getText().length() != 0) {
                            tag = true;
                        } else {
                            tag = false;
                            break;
                        }
                    }

                    if (tag) {
                        setBtnAvailable();
                    } else {
                        setBtnUnavailable();
                    }
                }
            });
        }


    }

    /**
     * 可以点击
     */
    private void setBtnAvailable() {

        btn.setBackground(ContextCompat.getDrawable(MyApplication.getInstance(), click));
        btn.setEnabled(true);
    }

    /**
     * 不能点击
     */
    private void setBtnUnavailable() {
        btn.setBackground(ContextCompat.getDrawable(MyApplication.getInstance(), noClick));
        btn.setEnabled(false);
    }
}
